package com.tmc.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.bean.intair.EthShoppingReq;
import com.tmc.constant.FareConstant.PolicyStatus;
import com.tmc.entity.intair.CommisionPolicy;
import com.tmc.util.DateUtil;
import com.tmc.util.ObjectUtil;

@Repository
public class CommisionPolicyDaoImpl extends BaseDaoImpl<CommisionPolicy> {
	
	public List<CommisionPolicy> findPolicys(ContextInfo context, EthShoppingReq req) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from CommisionPolicy c where c.enabledFlag = 'Y'");
		//匹配政策已上架
		sql.append(" and c.status= :status");
		params.put("status", PolicyStatus.UP.getStatus());
		// 匹配飞行类型
		sql.append(" and (c.flightType is null or c.flightType like :flightType )");
		params.put("flightType", "%" + req.getFlightType() + "%");
		//匹配开票时间
		sql.append(" and (c.ticketingDateStart <= :currentTime");
		sql.append(" and c.ticketingDateEnd >= :currentTime)");
		params.put("currentTime", DateUtil.toLongDate(//
				DateUtil.toStrDate(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd"));
		
		//匹配航司
		if (ObjectUtil.hasText(req.getAirline())) {
			sql.append(" and c.airline = :airline");
			params.put("airline", req.getAirline());
		}
		return this.findList(sql.toString(), params);
	}
	
	public List<CommisionPolicy> findListByPolicy(ContextInfo context, CommisionPolicy policy){
		Map<String,Object> params = new HashMap<String,Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from CommisionPolicy c where c.enabledFlag = 'Y'");
		if(ObjectUtil.hasText(policy.getFlightType())){
			sql.append(" and c.flightType like :flightType");
			params.put("flightType", "%"+policy.getFlightType()+"%");		
		}
		if(ObjectUtil.hasText(policy.getTicketType())){
			sql.append(" and c.ticketType = :ticketType");
			params.put("ticketType", "%"+policy.getTicketType()+"%");
		}		
		if(ObjectUtil.hasText(policy.getOfficeNo())){
			sql.append(" and c.officeNo = :officeNo");
			params.put("officeNo", policy.getOfficeNo());
		}
		if(ObjectUtil.hasText(policy.getStatus())){
			sql.append(" and c.status= :status");
			params.put("status", policy.getStatus());
		}
		//匹配开票时间
		sql.append(" and (c.ticketingDateStart <= :currentTime");
		sql.append(" and c.ticketingDateEnd >= :currentTime)");
		params.put("currentTime", DateUtil.toLongDate(//
				DateUtil.toStrDate(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd"));
		
		return this.findList(sql.toString(), params);	
	}

	public Pagination<CommisionPolicy> findPolicysByCon(ContextInfo context, CommisionPolicy cond, int pageIndex,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from CommisionPolicy c where c.enabledFlag = 'Y' ");

		if (ObjectUtil.hasText(cond.getDeparture())) {
			sql.append(" and (c.departure is null or c.departure like :departure ) ");
			params.put("departure", "%" + cond.getDeparture() + "%");
		}
		if (ObjectUtil.hasText(cond.getArrival())) {
			sql.append(" and (c.arrival is null or c.arrival like :arrival )");
			params.put("arrival", "%" + cond.getArrival() + "%");
		}
		if (ObjectUtil.hasText(cond.getOutboundTravelDateStartStr())) {
			sql.append(" and (c.outboundTravelDateStart is null or c.outboundTravelDateStart <= :startDate )");
			params.put("startDate", DateUtil.toLongDate(cond.getOutboundTravelDateStartStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getOutboundTravelDateEndStr())) {
			sql.append(" and (c.outboundTravelDateEnd is null or c.outboundTravelDateEnd >= :endDate )");
			params.put("endDate", DateUtil.toLongDate(cond.getOutboundTravelDateEndStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getAirline())) {
			sql.append(" and c.airline = :airline");
			params.put("airline", cond.getAirline());
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public CommisionPolicy getByOfficeNo(ContextInfo context,String officeNo) {
		if(!ObjectUtil.hasText(officeNo)){
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from CommisionPolicy c where c.enabledFlag = 'Y' ");
		sql.append(" and c.officeNo = :officeNo");
		params.put("officeNo", officeNo);
		return this.get(sql.toString(), params);
	}
	
}
